package view.student;

import controller.StatisticsController;
import controller.AdminStatisticsController;
import model.User;
import util.FontUtils;

import javax.swing.*;
import javax.swing.border.AbstractBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;
import java.awt.geom.GeneralPath;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 现代化统计面板（学生端和管理员端共用）
 */
public class StatisticsPanel extends JPanel {
    private User currentUser;
    private boolean isAdminMode;
    private StatisticsController statisticsController;
    private AdminStatisticsController adminStatisticsController;

    private JComboBox<String> periodComboBox;
    private JButton refreshButton;

    // 统计信息标签
    private JLabel totalReservationsLabel;
    private JLabel totalHoursLabel;
    private JLabel preferredTimeLabel;
    private JLabel efficiencyLabel;

    public StatisticsPanel(User user, boolean isAdminMode) {
        this.currentUser = user;
        this.isAdminMode = isAdminMode;
        this.statisticsController = new StatisticsController();

        if (isAdminMode) {
            this.adminStatisticsController = new AdminStatisticsController();
        }

        initComponents();
        setupLayout();
        setupEventListeners();
        refreshData();
    }

    private void initComponents() {
        setLayout(new BorderLayout());
        // 现代化渐变背景
        setBackground(new Color(247, 250, 255));

        if (!isAdminMode) {
            periodComboBox = new JComboBox<>(new String[] { "近一周", "近一月", "近一年" });
        } else {
            periodComboBox = new JComboBox<>(new String[] { "今日", "本周", "本月" });
        }

        refreshButton = new JButton("刷新数据");

        styleModernComboBox(periodComboBox);
        styleModernButton(refreshButton);

        // 学生端统计标签（现代化设计）
        if (!isAdminMode) {
            totalReservationsLabel = new JLabel("--");
            totalHoursLabel = new JLabel("--");
            preferredTimeLabel = new JLabel("--");
            efficiencyLabel = new JLabel("--");
        } else {
            totalReservationsLabel = createStyledLabel("总预约数: --");
            totalHoursLabel = createStyledLabel("今日预约: --");
            preferredTimeLabel = createStyledLabel("座位利用率: --%");
            efficiencyLabel = createStyledLabel("热门时段: --");
        }
    }

    private void setupLayout() {
        // 现代化顶部控制栏
        JPanel controlPanel = createModernControlPanel();
        add(controlPanel, BorderLayout.NORTH);

        if (!isAdminMode) {
            setupModernStudentLayout();
        } else {
            setupAdminLayout();
        }
    }

    private JPanel createModernControlPanel() {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 渐变背景
                GradientPaint gradient = new GradientPaint(0, 0, Color.WHITE, 0, getHeight(), new Color(248, 252, 255));
                g2d.setPaint(gradient);
                g2d.fillRect(0, 0, getWidth(), getHeight());

                // 底部分割线
                g2d.setColor(new Color(226, 232, 240));
                g2d.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
            }
        };

        panel.setLayout(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(20, 30, 20, 30));

        // 左侧：标题和描述
        JPanel leftPanel = new JPanel(new BorderLayout());
        leftPanel.setOpaque(false);

        JLabel titleLabel = new JLabel("📊 数据统计中心");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 24));
        titleLabel.setForeground(new Color(30, 41, 59));

        JLabel subLabel = new JLabel("深入了解您的学习数据和使用习惯");
        subLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        subLabel.setForeground(new Color(100, 116, 139));

        leftPanel.add(titleLabel, BorderLayout.NORTH);
        leftPanel.add(subLabel, BorderLayout.CENTER);

        // 右侧：控制组件
        JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 15, 0));
        rightPanel.setOpaque(false);

        JLabel periodLabel = new JLabel("时间范围");
        periodLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        periodLabel.setForeground(new Color(71, 85, 105));

        rightPanel.add(periodLabel);
        rightPanel.add(periodComboBox);
        rightPanel.add(refreshButton);

        panel.add(leftPanel, BorderLayout.WEST);
        panel.add(rightPanel, BorderLayout.EAST);

        return panel;
    }

    private void setupModernStudentLayout() {
        JPanel mainPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 创建渐变背景
                GradientPaint gradient = new GradientPaint(
                        0, 0, new Color(247, 250, 255),
                        0, getHeight(), new Color(240, 245, 255));
                g2d.setPaint(gradient);
                g2d.fillRect(0, 0, getWidth(), getHeight());
            }
        };

        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));

        // 主容器：4:6 比例
        JPanel contentPanel = new JPanel(new BorderLayout(25, 0));
        contentPanel.setOpaque(false);

        // 左侧面板 (40%) - 改为2x2方块卡片
        JPanel leftPanel = new JPanel(new BorderLayout(0, 20));
        leftPanel.setOpaque(false);
        leftPanel.setPreferredSize(new Dimension(400, 0));

        // 方块统计卡片区域
        JPanel squareCardsPanel = createSquareStatsCards();
        leftPanel.add(squareCardsPanel, BorderLayout.CENTER);

        // 右侧面板 (60%) - 改为2x2四个图表
        JPanel rightPanel = new JPanel(new BorderLayout(0, 20));
        rightPanel.setOpaque(false);

        // 四个图表的2x2布局
        JPanel chartsGrid = createFourChartsGrid();
        rightPanel.add(chartsGrid, BorderLayout.CENTER);

        contentPanel.add(leftPanel, BorderLayout.WEST);
        contentPanel.add(rightPanel, BorderLayout.CENTER);

        mainPanel.add(contentPanel, BorderLayout.CENTER);
        add(mainPanel, BorderLayout.CENTER);
    }

    private void setupAdminLayout() {
        JPanel mainPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 创建渐变背景
                GradientPaint gradient = new GradientPaint(
                        0, 0, new Color(247, 250, 255),
                        0, getHeight(), new Color(240, 245, 255));
                g2d.setPaint(gradient);
                g2d.fillRect(0, 0, getWidth(), getHeight());
            }
        };

        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));

        // 主容器：4:6 比例
        JPanel contentPanel = new JPanel(new BorderLayout(25, 0));
        contentPanel.setOpaque(false);

        // 左侧面板 (40%) - 改为2x2方块卡片
        JPanel leftPanel = new JPanel(new BorderLayout(0, 20));
        leftPanel.setOpaque(false);
        leftPanel.setPreferredSize(new Dimension(400, 0));

        // 方块统计卡片区域
        JPanel squareCardsPanel = createAdminSquareStatsCards();
        leftPanel.add(squareCardsPanel, BorderLayout.CENTER);

        // 右侧面板 (60%) - 改为2x2四个图表
        JPanel rightPanel = new JPanel(new BorderLayout(0, 20));
        rightPanel.setOpaque(false);

        // 四个图表的2x2布局
        JPanel chartsGrid = createAdminChartsGrid();
        rightPanel.add(chartsGrid, BorderLayout.CENTER);

        contentPanel.add(leftPanel, BorderLayout.WEST);
        contentPanel.add(rightPanel, BorderLayout.CENTER);

        mainPanel.add(contentPanel, BorderLayout.CENTER);
        add(mainPanel, BorderLayout.CENTER);
    }

    private JPanel createModernStatsOverview() {
        JPanel panel = new JPanel(new BorderLayout(0, 12));
        panel.setOpaque(false);

        // 标题
        JLabel titleLabel = new JLabel("📊 个人统计概览");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        titleLabel.setForeground(new Color(30, 41, 59));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));

        // 统计卡片容器
        JPanel cardsPanel = new JPanel(new GridLayout(2, 1, 0, 12));
        cardsPanel.setOpaque(false);
        cardsPanel.setPreferredSize(new Dimension(0, 240)); // 固定高度

        // 主要统计卡片
        JPanel totalReservationsCard = createPremiumStatCard(
                totalReservationsLabel, "📈", "总预约次数",
                new Color(99, 102, 241), new Color(165, 180, 252));

        JPanel totalHoursCard = createPremiumStatCard(
                totalHoursLabel, "⏱️", "总预约时长",
                new Color(16, 185, 129), new Color(110, 231, 183));

        cardsPanel.add(totalReservationsCard);
        cardsPanel.add(totalHoursCard);

        panel.add(titleLabel, BorderLayout.NORTH);
        panel.add(cardsPanel, BorderLayout.CENTER);

        return panel;
    }

    private JPanel createSecondaryStats() {
        JPanel panel = new JPanel(new BorderLayout(0, 12));
        panel.setOpaque(false);

        // 标题
        JLabel titleLabel = new JLabel("🎯 使用分析");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        titleLabel.setForeground(new Color(30, 41, 59));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));

        // 次要统计卡片
        JPanel cardsPanel = new JPanel(new GridLayout(2, 1, 0, 12));
        cardsPanel.setOpaque(false);
        cardsPanel.setPreferredSize(new Dimension(0, 240)); // 固定高度，与上面保持一致

        JPanel preferredTimeCard = createPremiumStatCard(
                preferredTimeLabel, "🌟", "偏好时段",
                new Color(139, 69, 19), new Color(251, 191, 36));

        JPanel efficiencyCard = createPremiumStatCard(
                efficiencyLabel, "⚡", "使用评级",
                new Color(168, 85, 247), new Color(196, 181, 253));

        cardsPanel.add(preferredTimeCard);
        cardsPanel.add(efficiencyCard);

        panel.add(titleLabel, BorderLayout.NORTH);
        panel.add(cardsPanel, BorderLayout.CENTER);

        return panel;
    }

    /**
     * 创建2x2方块统计卡片
     */
    private JPanel createSquareStatsCards() {
        JPanel panel = new JPanel(new BorderLayout(0, 15));
        panel.setOpaque(false);

        // 标题
        JLabel titleLabel = new JLabel("📊 个人统计概览");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        titleLabel.setForeground(new Color(30, 41, 59));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));

        // 2x2网格卡片容器
        JPanel cardsGrid = new JPanel(new GridLayout(2, 2, 15, 15));
        cardsGrid.setOpaque(false);

        // 四个方块统计卡片
        JPanel reservationCard = createSquareStatCard(
                totalReservationsLabel, "📈", "总预约次数",
                new Color(99, 102, 241), new Color(165, 180, 252));

        JPanel hoursCard = createSquareStatCard(
                totalHoursLabel, "⏱️", "总预约时长",
                new Color(16, 185, 129), new Color(110, 231, 183));

        JPanel preferredCard = createSquareStatCard(
                preferredTimeLabel, "🌟", "偏好时段",
                new Color(139, 69, 19), new Color(251, 191, 36));

        JPanel efficiencyCard = createSquareStatCard(
                efficiencyLabel, "⚡", "使用评级",
                new Color(168, 85, 247), new Color(196, 181, 253));

        cardsGrid.add(reservationCard);
        cardsGrid.add(hoursCard);
        cardsGrid.add(preferredCard);
        cardsGrid.add(efficiencyCard);

        panel.add(titleLabel, BorderLayout.NORTH);
        panel.add(cardsGrid, BorderLayout.CENTER);

        return panel;
    }

    /**
     * 创建管理员端2x2方块统计卡片
     */
    private JPanel createAdminSquareStatsCards() {
        JPanel panel = new JPanel(new BorderLayout(0, 15));
        panel.setOpaque(false);

        // 标题
        JLabel titleLabel = new JLabel("📊 系统统计概览");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        titleLabel.setForeground(new Color(30, 41, 59));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));

        // 2x2网格卡片容器
        JPanel cardsGrid = new JPanel(new GridLayout(2, 2, 15, 15));
        cardsGrid.setOpaque(false);

        // 四个方块统计卡片 - 使用管理员配色
        JPanel totalReservationsCard = createSquareStatCard(
                totalReservationsLabel, "📈", "总预约数",
                new Color(239, 68, 68), new Color(252, 165, 165)); // 红色主题

        JPanel todayReservationsCard = createSquareStatCard(
                totalHoursLabel, "📅", "今日预约",
                new Color(245, 158, 11), new Color(253, 230, 138)); // 橙色主题

        JPanel utilizationCard = createSquareStatCard(
                preferredTimeLabel, "📈", "座位利用率",
                new Color(16, 185, 129), new Color(110, 231, 183)); // 绿色主题

        JPanel popularTimeCard = createSquareStatCard(
                efficiencyLabel, "⏰", "热门时段",
                new Color(99, 102, 241), new Color(165, 180, 252)); // 紫色主题

        cardsGrid.add(totalReservationsCard);
        cardsGrid.add(todayReservationsCard);
        cardsGrid.add(utilizationCard);
        cardsGrid.add(popularTimeCard);

        panel.add(titleLabel, BorderLayout.NORTH);
        panel.add(cardsGrid, BorderLayout.CENTER);

        return panel;
    }

    /**
     * 创建管理员端2x2图表布局
     */
    private JPanel createAdminChartsGrid() {
        JPanel panel = new JPanel(new BorderLayout(0, 15));
        panel.setOpaque(false);

        // 标题
        JLabel titleLabel = new JLabel("📈 数据分析图表");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        titleLabel.setForeground(new Color(30, 41, 59));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));

        // 2x2图表网格
        JPanel chartsGrid = new JPanel(new GridLayout(2, 2, 15, 15));
        chartsGrid.setOpaque(false);

        // 四个图表 - 使用现有的管理员端图表
        JPanel trendChart = wrapAdminChartInModernCard(createTrendChart(), "系统预约趋势");
        JPanel distributionChart = wrapAdminChartInModernCard(createTimeDistributionChart(), "时间段分布");
        JPanel floorChart = wrapAdminChartInModernCard(createFloorChart(), "区域使用率");
        JPanel heatmapChart = wrapAdminChartInModernCard(createHeatmapChart(), "使用热力图");

        chartsGrid.add(trendChart);
        chartsGrid.add(distributionChart);
        chartsGrid.add(floorChart);
        chartsGrid.add(heatmapChart);

        panel.add(titleLabel, BorderLayout.NORTH);
        panel.add(chartsGrid, BorderLayout.CENTER);

        return panel;
    }

    /**
     * 将管理员端图表包装成现代化卡片
     */
    private JPanel wrapAdminChartInModernCard(JPanel chartPanel, String title) {
        JPanel wrapper = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 背景
                g2d.setColor(Color.WHITE);
                g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16);

                // 阴影
                g2d.setColor(new Color(0, 0, 0, 8));
                g2d.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 16, 16);
            }
        };

        wrapper.setLayout(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        wrapper.setPreferredSize(new Dimension(0, 280)); // 固定图表高度

        // 添加标题
        JLabel titleLabel = new JLabel(title, JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        titleLabel.setForeground(new Color(75, 85, 99));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));

        // 移除原图表的标题和边框
        if (chartPanel.getLayout() instanceof BorderLayout) {
            BorderLayout layout = (BorderLayout) chartPanel.getLayout();
            Component titleComponent = layout.getLayoutComponent(BorderLayout.NORTH);
            if (titleComponent != null) {
                chartPanel.remove(titleComponent);
            }
        }
        chartPanel.setBorder(null);
        chartPanel.setBackground(Color.WHITE);

        wrapper.add(titleLabel, BorderLayout.NORTH);
        wrapper.add(chartPanel, BorderLayout.CENTER);

        return wrapper;
    }

    /**
     * 创建2x2四个图表布局
     */
    private JPanel createFourChartsGrid() {
        JPanel panel = new JPanel(new BorderLayout(0, 15));
        panel.setOpaque(false);

        // 标题
        JLabel titleLabel = new JLabel("📈 数据分析图表");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        titleLabel.setForeground(new Color(30, 41, 59));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));

        // 2x2图表网格
        JPanel chartsGrid = new JPanel(new GridLayout(2, 2, 15, 15));
        chartsGrid.setOpaque(false);

        // 四个图表
        JPanel trendChart = createCompactTrendChart(); // 预约趋势图
        JPanel pieChart = createCompactPieChart(); // 时间段偏好图
        JPanel creditChart = createCreditChart(); // 信誉分图表
        JPanel attendanceChart = createAttendanceChart(); // 考勤图表

        chartsGrid.add(trendChart);
        chartsGrid.add(pieChart);
        chartsGrid.add(creditChart);
        chartsGrid.add(attendanceChart);

        panel.add(titleLabel, BorderLayout.NORTH);
        panel.add(chartsGrid, BorderLayout.CENTER);

        return panel;
    }

    private void setupEventListeners() {
        refreshButton.addActionListener(e -> refreshData());
        periodComboBox.addActionListener(e -> refreshData());
    }

    public void refreshData() {
        try {
            Map<String, Object> stats;
            if (isAdminMode) {
                stats = adminStatisticsController.getSystemOverviewStats();
            } else {
                stats = statisticsController.getUserStatistics(currentUser.getUserId(), isAdminMode);
            }
            updateStatisticsLabels(stats);

            // Charts are redrawn on period change or refresh
            repaint();
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "加载统计数据失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void updateStatisticsLabels(Map<String, Object> stats) {
        if (isAdminMode) {
            totalReservationsLabel.setText("总预约数: " + stats.getOrDefault("totalReservations", 0));
            totalHoursLabel.setText("今日预约: " + stats.getOrDefault("todayReservations", 0));
            double utilization = ((Number) stats.getOrDefault("seatUtilizationRate", 0.0)).doubleValue();
            preferredTimeLabel.setText("座位利用率: " + String.format("%.1f", utilization) + "%");
            efficiencyLabel.setText("热门时段: " + stats.getOrDefault("popularTimeSlot", "--"));
        } else {
            // 学生端现代化数据显示
            int reservations = (Integer) stats.getOrDefault("myReservations", 0);
            int hours = (Integer) stats.getOrDefault("totalHours", 0);
            String preferredTime = stats.getOrDefault("preferredTime", "暂无数据").toString();

            totalReservationsLabel.setText(String.valueOf(reservations));
            totalHoursLabel.setText(hours + " 小时");
            preferredTimeLabel.setText(preferredTime);

            // 计算使用效率评级
            String efficiency = calculateEfficiency(reservations, hours);
            efficiencyLabel.setText(efficiency);
        }
    }

    private String calculateEfficiency(int reservations, int hours) {
        if (reservations == 0)
            return "新用户";
        double avgHours = (double) hours / reservations;
        if (avgHours >= 4)
            return "高效学习者";
        else if (avgHours >= 2)
            return "积极用户";
        else
            return "轻度用户";
    }

    private JPanel createModernTrendChart() {
        JPanel containerPanel = new JPanel(new BorderLayout());
        containerPanel.setOpaque(false);

        // 标题栏
        JPanel headerPanel = new JPanel(new BorderLayout());
        headerPanel.setOpaque(false);
        headerPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 12, 20));

        JLabel titleLabel = new JLabel("📈 预约趋势分析");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        titleLabel.setForeground(new Color(30, 41, 59));

        JLabel descLabel = new JLabel("查看您在不同时间段的预约时长变化");
        descLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
        descLabel.setForeground(new Color(100, 116, 139));

        JPanel titleContainer = new JPanel(new BorderLayout());
        titleContainer.setOpaque(false);
        titleContainer.add(titleLabel, BorderLayout.NORTH);
        titleContainer.add(descLabel, BorderLayout.CENTER);

        headerPanel.add(titleContainer, BorderLayout.WEST);

        // 图表面板
        JPanel chartPanel = createPremiumTrendChart();

        containerPanel.add(headerPanel, BorderLayout.NORTH);
        containerPanel.add(chartPanel, BorderLayout.CENTER);

        return wrapInModernCard(containerPanel);
    }

    private JPanel createModernPieChart() {
        JPanel containerPanel = new JPanel(new BorderLayout());
        containerPanel.setOpaque(false);

        // 标题栏
        JPanel headerPanel = new JPanel(new BorderLayout());
        headerPanel.setOpaque(false);
        headerPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 12, 20));

        JLabel titleLabel = new JLabel("🥧 时间段偏好分布");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        titleLabel.setForeground(new Color(30, 41, 59));

        JLabel descLabel = new JLabel("了解您在各个时间段的使用习惯");
        descLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
        descLabel.setForeground(new Color(100, 116, 139));

        JPanel titleContainer = new JPanel(new BorderLayout());
        titleContainer.setOpaque(false);
        titleContainer.add(titleLabel, BorderLayout.NORTH);
        titleContainer.add(descLabel, BorderLayout.CENTER);

        headerPanel.add(titleContainer, BorderLayout.WEST);

        // 图表面板
        JPanel chartPanel = createPremiumPieChart();

        containerPanel.add(headerPanel, BorderLayout.NORTH);
        containerPanel.add(chartPanel, BorderLayout.CENTER);

        return wrapInModernCard(containerPanel);
    }

    private JPanel createStudentTrendChart() {
        JPanel panel = new JPanel() {
            private int hoveredPoint = -1;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                int width = getWidth() - 80;
                int height = getHeight() - 120;

                // 绘制背景网格
                g2d.setColor(new Color(240, 240, 240));
                g2d.setStroke(new BasicStroke(1));
                for (int i = 0; i <= 5; i++) {
                    int y = 40 + i * (height / 5);
                    g2d.drawLine(40, y, width + 40, y);
                }

                // 获取当前选择的时间范围
                String period = (String) periodComboBox.getSelectedItem();
                String[] timeLabels;
                int[] dataPoints;

                // 根据时间范围生成数据
                switch (period) {
                    case "近一周":
                        timeLabels = new String[] { "周一", "周二", "周三", "周四", "周五", "周六", "周日" };
                        dataPoints = new int[] { 2, 4, 3, 5, 6, 3, 4 }; // 预约时长（小时）
                        break;
                    case "近一月":
                        timeLabels = new String[] { "第1周", "第2周", "第3周", "第4周" };
                        dataPoints = new int[] { 18, 25, 22, 28 }; // 周累计时长
                        break;
                    case "近一年":
                        timeLabels = new String[] { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月",
                                "12月" };
                        dataPoints = new int[] { 80, 75, 90, 85, 95, 100, 88, 92, 87, 90, 85, 88 }; // 月累计时长
                        break;
                    default:
                        timeLabels = new String[] { "周一", "周二", "周三", "周四", "周五", "周六", "周日" };
                        dataPoints = new int[] { 2, 4, 3, 5, 6, 3, 4 };
                }

                // 绘制垂直网格线
                if (timeLabels.length > 1) {
                    for (int i = 0; i <= timeLabels.length - 1; i++) {
                        int x = 40 + (i * width / (timeLabels.length - 1));
                        g2d.drawLine(x, 40, x, height + 40);
                    }
                }

                // 绘制折线图
                g2d.setColor(new Color(59, 130, 246));
                g2d.setStroke(new BasicStroke(3));

                int maxValue = 0;
                for (int value : dataPoints) {
                    maxValue = Math.max(maxValue, value);
                }
                maxValue = Math.max(maxValue, 10); // 最小刻度

                if (dataPoints.length > 1) {
                    for (int i = 0; i < dataPoints.length - 1; i++) {
                        int x1 = 40 + (i * width / (dataPoints.length - 1));
                        int y1 = height - (dataPoints[i] * height / maxValue) + 40;
                        int x2 = 40 + ((i + 1) * width / (dataPoints.length - 1));
                        int y2 = height - (dataPoints[i + 1] * height / maxValue) + 40;
                        g2d.drawLine(x1, y1, x2, y2);
                    }
                }

                // 绘制数据点
                for (int i = 0; i < dataPoints.length; i++) {
                    int x = 40 + (i * width / (Math.max(1, dataPoints.length - 1)));
                    int y = height - (dataPoints[i] * height / maxValue) + 40;

                    if (i == hoveredPoint) {
                        g2d.setColor(new Color(239, 68, 68));
                        g2d.fillOval(x - 8, y - 8, 16, 16);

                        // 显示数值
                        g2d.setColor(Color.BLACK);
                        g2d.setFont(new Font("Microsoft YaHei", Font.BOLD, 12));
                        g2d.drawString(dataPoints[i] + "小时", x - 20, y - 15);
                    } else {
                        g2d.setColor(new Color(59, 130, 246));
                        g2d.fillOval(x - 5, y - 5, 10, 10);
                    }

                    // 时间标签
                    g2d.setColor(new Color(107, 114, 128));
                    g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
                    int stringWidth = g2d.getFontMetrics().stringWidth(timeLabels[i]);
                    g2d.drawString(timeLabels[i], x - stringWidth / 2, height + 65);
                }

                // Y轴标签
                g2d.setColor(new Color(107, 114, 128));
                g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 10));
                for (int i = 0; i <= 5; i++) {
                    int value = (maxValue * i / 5);
                    int y = height - (i * height / 5) + 40;
                    g2d.drawString(value + "h", 10, y + 5);
                }
            }
        };

        // 添加鼠标交互
        panel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseMoved(java.awt.event.MouseEvent e) {
                String period = (String) periodComboBox.getSelectedItem();
                int dataLength = period.equals("近一年") ? 12 : (period.equals("近一月") ? 4 : 7);

                int width = panel.getWidth() - 80;
                int newHovered = -1;

                if (dataLength > 1) {
                    for (int i = 0; i < dataLength; i++) {
                        int x = 40 + (i * width / (dataLength - 1));
                        if (Math.abs(e.getX() - x) < 20) {
                            newHovered = i;
                            break;
                        }
                    }
                }

                Integer oldHovered = (Integer) panel.getClientProperty("hoveredPoint");
                if (oldHovered == null)
                    oldHovered = -1;
                if (newHovered != oldHovered) {
                    panel.putClientProperty("hoveredPoint", newHovered);
                    panel.repaint();
                }
            }
        });

        panel.setBackground(new Color(255, 255, 255));
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(229, 231, 235), 1),
                BorderFactory.createEmptyBorder(15, 15, 15, 15)));

        // 添加标题
        JLabel titleLabel = new JLabel("预约趋势图", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 18));
        titleLabel.setForeground(new Color(75, 85, 99));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));

        panel.setLayout(new BorderLayout());
        panel.add(titleLabel, BorderLayout.NORTH);

        return panel;
    }

    private JPanel createTimePreferencePieChart() {
        JPanel panel = new JPanel() {
            private int hoveredSegment = -1;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                int centerX = getWidth() / 2;
                int centerY = (getHeight() - 80) / 2 + 40;
                int radius = Math.min(getWidth() - 100, getHeight() - 120) / 2;

                String[] timeSlots = { "上午(8-12)", "下午(12-18)", "晚上(18-22)" };
                int[] values = { 25, 35, 40 }; // 百分比
                Color[] colors = {
                        new Color(34, 197, 94), // 绿色
                        new Color(245, 158, 11), // 橙色
                        new Color(59, 130, 246) // 蓝色
                };

                int startAngle = 0;
                for (int i = 0; i < timeSlots.length; i++) {
                    int arcAngle = (int) (360.0 * values[i] / 100);

                    // 如果是悬停的扇形，稍微突出显示
                    int drawRadius = radius;
                    int drawCenterX = centerX;
                    int drawCenterY = centerY;

                    if (i == hoveredSegment) {
                        // 突出显示效果
                        double midAngle = Math.toRadians(startAngle + arcAngle / 2.0);
                        drawCenterX += (int) (15 * Math.cos(midAngle));
                        drawCenterY += (int) (15 * Math.sin(midAngle));
                        g2d.setColor(colors[i].brighter());
                    } else {
                        g2d.setColor(colors[i]);
                    }

                    g2d.fillArc(drawCenterX - drawRadius, drawCenterY - drawRadius,
                            2 * drawRadius, 2 * drawRadius, startAngle, arcAngle);

                    // 绘制边框
                    g2d.setColor(Color.WHITE);
                    g2d.setStroke(new BasicStroke(3));
                    g2d.drawArc(drawCenterX - drawRadius, drawCenterY - drawRadius,
                            2 * drawRadius, 2 * drawRadius, startAngle, arcAngle);

                    // 在扇形内显示百分比
                    double midAngle = Math.toRadians(startAngle + arcAngle / 2.0);
                    int textX = drawCenterX + (int) (radius * 0.7 * Math.cos(midAngle));
                    int textY = drawCenterY + (int) (radius * 0.7 * Math.sin(midAngle));

                    g2d.setColor(Color.WHITE);
                    g2d.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
                    String percentage = values[i] + "%";
                    int stringWidth = g2d.getFontMetrics().stringWidth(percentage);
                    g2d.drawString(percentage, textX - stringWidth / 2, textY + 5);

                    startAngle += arcAngle;
                }

                // 绘制现代化图例
                int legendY = getHeight() - 80;
                int legendX = 20;
                for (int i = 0; i < timeSlots.length; i++) {
                    g2d.setColor(colors[i]);
                    g2d.fillRoundRect(legendX, legendY + i * 25, 15, 15, 4, 4);

                    g2d.setColor(new Color(75, 85, 99));
                    g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
                    g2d.drawString(timeSlots[i] + " - " + values[i] + "%", legendX + 25, legendY + i * 25 + 12);
                }
            }
        };

        // 添加鼠标交互
        panel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseMoved(java.awt.event.MouseEvent e) {
                int centerX = panel.getWidth() / 2;
                int centerY = (panel.getHeight() - 80) / 2 + 40;
                int radius = Math.min(panel.getWidth() - 100, panel.getHeight() - 120) / 2;

                // 计算鼠标相对于中心的角度
                int dx = e.getX() - centerX;
                int dy = e.getY() - centerY;
                double distance = Math.sqrt(dx * dx + dy * dy);

                int newHovered = -1;
                if (distance <= radius) {
                    double angle = Math.toDegrees(Math.atan2(dy, dx));
                    if (angle < 0)
                        angle += 360;

                    int[] values = { 25, 35, 40 };
                    int currentAngle = 0;
                    for (int i = 0; i < values.length; i++) {
                        int arcAngle = (int) (360.0 * values[i] / 100);
                        if (angle >= currentAngle && angle < currentAngle + arcAngle) {
                            newHovered = i;
                            break;
                        }
                        currentAngle += arcAngle;
                    }
                }

                Integer oldHovered = (Integer) panel.getClientProperty("hoveredSegment");
                if (oldHovered == null)
                    oldHovered = -1;
                if (newHovered != oldHovered) {
                    panel.putClientProperty("hoveredSegment", newHovered);
                    panel.repaint();
                }
            }
        });

        panel.setBackground(new Color(255, 255, 255));
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(229, 231, 235), 1),
                BorderFactory.createEmptyBorder(15, 15, 15, 15)));

        // 添加标题
        JLabel titleLabel = new JLabel("时间段偏好分布", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 18));
        titleLabel.setForeground(new Color(75, 85, 99));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));

        panel.setLayout(new BorderLayout());
        panel.add(titleLabel, BorderLayout.NORTH);

        return panel;
    }

    private JPanel createTrendChart() {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 设置背景
                g2d.setColor(Color.WHITE);
                g2d.fillRect(0, 0, getWidth(), getHeight());

                int width = getWidth() - 60;
                int height = getHeight() - 80;
                int startX = 30, startY = 40;

                // 绘制网格
                g2d.setColor(new Color(240, 240, 240));
                g2d.setStroke(new BasicStroke(1));
                for (int i = 0; i <= 5; i++) {
                    int y = startY + i * (height / 5);
                    g2d.drawLine(startX, y, width + startX, y);
                }

                // 管理员端：从数据库获取真实数据
                String period = (String) periodComboBox.getSelectedItem();
                String[] labels;
                int[] data;

                try {
                    Map<String, Object> trendData = adminStatisticsController.getSystemTrendData(period);
                    labels = (String[]) trendData.get("labels");
                    data = (int[]) trendData.get("data");
                } catch (Exception e) {
                    // 使用默认数据作为fallback
                    switch (period) {
                        case "今日":
                            labels = new String[] { "8:00", "10:00", "12:00", "14:00", "16:00", "18:00", "20:00" };
                            data = new int[7];
                            break;
                        case "本周":
                            labels = new String[] { "周一", "周二", "周三", "周四", "周五", "周六", "周日" };
                            data = new int[7];
                            break;
                        case "本月":
                            labels = new String[] { "第1周", "第2周", "第3周", "第4周" };
                            data = new int[4];
                            break;
                        default:
                            labels = new String[] { "周一", "周二", "周三", "周四", "周五", "周六", "周日" };
                            data = new int[7];
                            break;
                    }
                }

                int maxValue = java.util.Arrays.stream(data).max().orElse(0);
                if (maxValue == 0)
                    maxValue = 10;

                // 绘制折线
                g2d.setColor(new Color(239, 68, 68)); // 管理员红色主题
                g2d.setStroke(new BasicStroke(3));

                for (int i = 0; i < data.length - 1; i++) {
                    int x1 = startX + (i * width / (data.length - 1));
                    int y1 = height - (data[i] * height / maxValue) + startY;
                    int x2 = startX + ((i + 1) * width / (data.length - 1));
                    int y2 = height - (data[i + 1] * height / maxValue) + startY;
                    g2d.drawLine(x1, y1, x2, y2);
                }

                // 绘制数据点
                for (int i = 0; i < data.length; i++) {
                    int x = startX + (i * width / (Math.max(1, data.length - 1)));
                    int y = height - (data[i] * height / maxValue) + startY;

                    g2d.setColor(Color.WHITE);
                    g2d.fillOval(x - 4, y - 4, 8, 8);
                    g2d.setColor(new Color(239, 68, 68));
                    g2d.setStroke(new BasicStroke(2));
                    g2d.drawOval(x - 4, y - 4, 8, 8);

                    // X轴标签
                    g2d.setColor(new Color(107, 114, 128));
                    g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 10));
                    int labelWidth = g2d.getFontMetrics().stringWidth(labels[i]);
                    g2d.drawString(labels[i], x - labelWidth / 2, height + startY + 20);
                }

                // Y轴标签
                g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 9));
                for (int i = 0; i <= 5; i++) {
                    int value = (maxValue * i / 5);
                    int y = height - (i * height / 5) + startY;
                    g2d.drawString(value + "", 5, y + 3);
                }
            }
        };

        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(229, 231, 235), 1),
                BorderFactory.createEmptyBorder(10, 10, 10, 10)));

        // 添加标题
        JLabel titleLabel = new JLabel("系统预约趋势", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        titleLabel.setForeground(new Color(75, 85, 99));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));

        panel.setLayout(new BorderLayout());
        panel.add(titleLabel, BorderLayout.NORTH);

        return panel;
    }

    private JPanel createTimeDistributionChart() {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 设置背景
                g2d.setColor(Color.WHITE);
                g2d.fillRect(0, 0, getWidth(), getHeight());

                int centerX = getWidth() / 2;
                int centerY = (getHeight() - 60) / 2 + 30;
                int radius = Math.min(getWidth() - 80, getHeight() - 80) / 2;

                // 管理员端：从数据库获取时间段预约分布
                String period = (String) periodComboBox.getSelectedItem();
                String[] timeSlots = { "上午", "下午", "晚上" };
                int[] values;

                try {
                    Map<String, Object> timeSlotData = adminStatisticsController.getSystemTimeSlotData(period);
                    values = (int[]) timeSlotData.get("data");
                } catch (Exception e) {
                    // 使用默认数据作为fallback
                    values = new int[] { 0, 0, 0 };
                }
                Color[] colors = {
                        new Color(239, 68, 68), // 红色
                        new Color(245, 158, 11), // 橙色
                        new Color(156, 163, 175) // 灰色
                };

                int startAngle = 90; // 从顶部开始
                for (int i = 0; i < timeSlots.length; i++) {
                    int arcAngle = (int) (360.0 * values[i] / 100);

                    g2d.setColor(colors[i]);
                    g2d.fillArc(centerX - radius, centerY - radius,
                            2 * radius, 2 * radius, startAngle, arcAngle);

                    // 绘制边框
                    g2d.setColor(Color.WHITE);
                    g2d.setStroke(new BasicStroke(2));
                    g2d.drawArc(centerX - radius, centerY - radius,
                            2 * radius, 2 * radius, startAngle, arcAngle);

                    // 在扇形内显示百分比
                    double midAngle = Math.toRadians(startAngle + arcAngle / 2.0);
                    int textX = centerX + (int) (radius * 0.6 * Math.cos(midAngle));
                    int textY = centerY - (int) (radius * 0.6 * Math.sin(midAngle));

                    g2d.setColor(Color.WHITE);
                    g2d.setFont(new Font("Microsoft YaHei", Font.BOLD, 12));
                    String percentage = values[i] + "%";
                    int stringWidth = g2d.getFontMetrics().stringWidth(percentage);
                    g2d.drawString(percentage, textX - stringWidth / 2, textY + 4);

                    startAngle += arcAngle;
                }

                // 绘制图例
                int legendY = getHeight() - 50;
                int legendX = 10;
                g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
                for (int i = 0; i < timeSlots.length; i++) {
                    g2d.setColor(colors[i]);
                    g2d.fillRoundRect(legendX + i * 70, legendY, 12, 12, 3, 3);

                    g2d.setColor(new Color(75, 85, 99));
                    g2d.drawString(timeSlots[i], legendX + i * 70 + 18, legendY + 10);
                }
            }
        };

        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(229, 231, 235), 1),
                BorderFactory.createEmptyBorder(10, 10, 10, 10)));

        // 添加标题
        JLabel titleLabel = new JLabel("时间段分布", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        titleLabel.setForeground(new Color(75, 85, 99));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));

        panel.setLayout(new BorderLayout());
        panel.add(titleLabel, BorderLayout.NORTH);

        return panel;
    }

    private JPanel createFloorChart() {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 设置背景
                g2d.setColor(Color.WHITE);
                g2d.fillRect(0, 0, getWidth(), getHeight());

                int width = getWidth() - 60;
                int height = getHeight() - 80;
                int startX = 30, startY = 40;

                // 管理员端：从数据库获取各区域使用情况
                String period = (String) periodComboBox.getSelectedItem();
                String[] areas;
                int[] usage;

                try {
                    Map<String, Object> areaData = adminStatisticsController.getAreaDistributionData(period);
                    areas = (String[]) areaData.get("labels");
                    usage = (int[]) areaData.get("data");
                } catch (Exception e) {
                    // 使用默认数据作为fallback
                    areas = new String[] { "A区", "C区", "图书馆1", "图书馆2", "图书馆3", "T16", "T3" };
                    usage = new int[areas.length]; // 全部为0
                }
                Color[] colors = {
                        new Color(239, 68, 68), // 红色
                        new Color(245, 158, 11), // 橙色
                        new Color(34, 197, 94), // 绿色
                        new Color(59, 130, 246), // 蓝色
                        new Color(168, 85, 247), // 紫色
                        new Color(156, 163, 175), // 灰色
                        new Color(251, 191, 36) // 黄色
                };

                int maxValue = java.util.Arrays.stream(usage).max().orElse(1); // 动态计算最大值
                if (maxValue == 0)
                    maxValue = 1; // 避免除零
                int barWidth = areas.length > 0 ? width / areas.length - 10 : 30;

                // 绘制柱状图
                for (int i = 0; i < areas.length; i++) {
                    int x = startX + i * (width / areas.length) + 5;
                    int barHeight = (usage[i] * height / maxValue);
                    int y = startY + height - barHeight;

                    // 绘制柱子
                    g2d.setColor(colors[i % colors.length]);
                    g2d.fillRoundRect(x, y, barWidth, barHeight, 4, 4);

                    // 绘制边框
                    g2d.setColor(colors[i % colors.length].darker());
                    g2d.setStroke(new BasicStroke(1));
                    g2d.drawRoundRect(x, y, barWidth, barHeight, 4, 4);

                    // 显示数值
                    g2d.setColor(Color.WHITE);
                    g2d.setFont(new Font("Microsoft YaHei", Font.BOLD, 10));
                    String valueText = String.valueOf(usage[i]);
                    int textWidth = g2d.getFontMetrics().stringWidth(valueText);
                    g2d.drawString(valueText, x + (barWidth - textWidth) / 2, y + barHeight / 2 + 3);

                    // X轴标签
                    g2d.setColor(new Color(107, 114, 128));
                    g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 9));
                    int labelWidth = g2d.getFontMetrics().stringWidth(areas[i]);
                    g2d.drawString(areas[i], x + (barWidth - labelWidth) / 2, startY + height + 15);
                }

                // 绘制Y轴标签
                g2d.setColor(new Color(107, 114, 128));
                g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 9));
                for (int i = 0; i <= 5; i++) {
                    int value = (maxValue * i / 5);
                    int y = startY + height - (i * height / 5);
                    g2d.drawString(value + "%", 5, y + 3);
                }
            }
        };

        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(229, 231, 235), 1),
                BorderFactory.createEmptyBorder(10, 10, 10, 10)));

        // 添加标题
        JLabel titleLabel = new JLabel("区域使用率", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        titleLabel.setForeground(new Color(75, 85, 99));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));

        panel.setLayout(new BorderLayout());
        panel.add(titleLabel, BorderLayout.NORTH);

        return panel;
    }

    private JPanel createHeatmapChart() {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 设置背景
                g2d.setColor(Color.WHITE);
                g2d.fillRect(0, 0, getWidth(), getHeight());

                int width = getWidth() - 60;
                int height = getHeight() - 100;
                int startX = 30, startY = 50;

                // 管理员端：从数据库获取热力图数据
                String period = (String) periodComboBox.getSelectedItem();
                String[] days, times;
                int[][] heatData;

                try {
                    Map<String, Object> heatmapDataMap = adminStatisticsController.getHeatmapData(period);
                    heatData = (int[][]) heatmapDataMap.get("data");

                    if (period.equals("今日")) {
                        times = new String[] { "上午", "下午", "晚上" };
                        days = new String[] { "今天" };
                    } else {
                        days = new String[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
                        times = new String[] { "上午", "下午", "晚上" };
                    }
                } catch (Exception e) {
                    // 使用默认数据作为fallback
                    switch (period) {
                        case "今日":
                            days = new String[] { "今天" };
                            times = new String[] { "上午", "下午", "晚上" };
                            heatData = new int[][] { { 0, 0, 0 } };
                            break;
                        case "本周":
                        default:
                            days = new String[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
                            times = new String[] { "上午", "下午", "晚上" };
                            heatData = new int[7][3]; // 全部为0
                            break;
                    }
                }

                int cellWidth = width / days.length;
                int cellHeight = height / times.length;

                // 绘制热力图
                for (int row = 0; row < times.length; row++) {
                    for (int col = 0; col < days.length; col++) {
                        int x = startX + col * cellWidth;
                        int y = startY + row * cellHeight;

                        // 根据数值计算颜色强度（红色系）
                        int intensity = heatData[col][row];
                        Color cellColor = new Color(
                                255,
                                Math.max(0, 255 - intensity * 2),
                                Math.max(0, 255 - intensity * 2),
                                200);

                        g2d.setColor(cellColor);
                        g2d.fillRoundRect(x + 2, y + 2, cellWidth - 4, cellHeight - 4, 6, 6);

                        // 绘制边框
                        g2d.setColor(new Color(229, 231, 235));
                        g2d.setStroke(new BasicStroke(1));
                        g2d.drawRoundRect(x + 2, y + 2, cellWidth - 4, cellHeight - 4, 6, 6);

                        // 显示数值
                        g2d.setColor(intensity > 50 ? Color.WHITE : new Color(75, 85, 99));
                        g2d.setFont(new Font("Microsoft YaHei", Font.BOLD, 10));
                        String valueText = intensity + "%";
                        int textWidth = g2d.getFontMetrics().stringWidth(valueText);
                        g2d.drawString(valueText,
                                x + (cellWidth - textWidth) / 2,
                                y + cellHeight / 2 + 3);
                    }
                }

                // X轴标签（天）
                g2d.setColor(new Color(107, 114, 128));
                g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 10));
                for (int i = 0; i < days.length; i++) {
                    int x = startX + i * cellWidth;
                    int labelWidth = g2d.getFontMetrics().stringWidth(days[i]);
                    g2d.drawString(days[i], x + (cellWidth - labelWidth) / 2, startY + height + 20);
                }

                // Y轴标签（时段）
                for (int i = 0; i < times.length; i++) {
                    int y = startY + i * cellHeight;
                    g2d.drawString(times[i], 5, y + cellHeight / 2 + 3);
                }

                // 颜色图例
                int legendX = startX + width - 40;
                int legendY = startY;
                g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 8));
                g2d.drawString("高", legendX + 15, legendY + 10);
                for (int i = 0; i < 20; i++) {
                    Color legendColor = new Color(255, 255 - i * 10, 255 - i * 10, 200);
                    g2d.setColor(legendColor);
                    g2d.fillRect(legendX, legendY + 15 + i * 2, 10, 2);
                }
                g2d.setColor(new Color(107, 114, 128));
                g2d.drawString("低", legendX + 15, legendY + 60);
            }
        };

        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(229, 231, 235), 1),
                BorderFactory.createEmptyBorder(10, 10, 10, 10)));

        // 添加标题
        JLabel titleLabel = new JLabel("使用热力图", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        titleLabel.setForeground(new Color(75, 85, 99));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));

        panel.setLayout(new BorderLayout());
        panel.add(titleLabel, BorderLayout.NORTH);

        return panel;
    }

    private JPanel createPremiumStatCard(JLabel dataLabel, String icon, String title, Color primaryColor,
            Color secondaryColor) {
        JPanel card = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 背景渐变
                GradientPaint gradient = new GradientPaint(
                        0, 0, Color.WHITE,
                        0, getHeight(), new Color(252, 252, 255));
                g2d.setPaint(gradient);
                g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16);

                // 左侧彩色装饰条
                GradientPaint accentGradient = new GradientPaint(
                        0, 0, primaryColor,
                        0, getHeight(), secondaryColor);
                g2d.setPaint(accentGradient);
                g2d.fillRoundRect(0, 0, 4, getHeight(), 4, 4);

                // 阴影效果
                g2d.setColor(new Color(0, 0, 0, 12));
                g2d.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 16, 16);
            }
        };

        card.setLayout(new BorderLayout(12, 6));
        card.setBorder(BorderFactory.createEmptyBorder(16, 20, 16, 16));
        card.setPreferredSize(new Dimension(0, 114)); // 固定高度

        // 图标区域
        JPanel iconPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        iconPanel.setOpaque(false);

        JLabel iconLabel = new JLabel(icon);
        iconLabel.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 24));
        iconPanel.add(iconLabel);

        // 内容区域
        JPanel contentPanel = new JPanel(new BorderLayout(0, 6));
        contentPanel.setOpaque(false);

        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
        titleLabel.setForeground(new Color(100, 116, 139));

        dataLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        dataLabel.setForeground(new Color(30, 41, 59));

        contentPanel.add(titleLabel, BorderLayout.NORTH);
        contentPanel.add(dataLabel, BorderLayout.CENTER);

        card.add(iconPanel, BorderLayout.WEST);
        card.add(contentPanel, BorderLayout.CENTER);

        // 悬停效果
        card.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseEntered(java.awt.event.MouseEvent e) {
                card.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(java.awt.event.MouseEvent e) {
                card.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
        });

        return card;
    }

    private JPanel wrapInModernCard(JPanel content) {
        JPanel wrapper = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 背景
                g2d.setColor(Color.WHITE);
                g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16);

                // 阴影
                g2d.setColor(new Color(0, 0, 0, 8));
                g2d.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 16, 16);
            }
        };

        wrapper.setLayout(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
        wrapper.setPreferredSize(new Dimension(0, 280)); // 固定图表高度
        wrapper.add(content, BorderLayout.CENTER);

        return wrapper;
    }

    private void styleModernComboBox(JComboBox<String> comboBox) {
        comboBox.setPreferredSize(new Dimension(140, 38));
        comboBox.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        comboBox.setBackground(Color.WHITE);
        comboBox.setForeground(new Color(71, 85, 105));
        comboBox.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(203, 213, 225), 1),
                BorderFactory.createEmptyBorder(8, 12, 8, 12)));
        comboBox.setFocusable(false);
    }

    private void styleModernButton(JButton button) {
        button.setPreferredSize(new Dimension(130, 38));
        button.setBackground(new Color(99, 102, 241));
        button.setForeground(Color.WHITE);
        button.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        button.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
        button.setFocusPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

        // 悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                if (button.isEnabled()) {
                    button.setBackground(new Color(79, 70, 229));
                }
            }

            @Override
            public void mouseExited(java.awt.event.MouseEvent evt) {
                if (button.isEnabled()) {
                    button.setBackground(new Color(99, 102, 241));
                }
            }
        });
    }

    private JPanel createModernStatCard(JLabel dataLabel, String icon, String title, Color accentColor) {
        JPanel card = new JPanel(new BorderLayout(0, 10));
        card.setBackground(Color.WHITE);
        card.setBorder(BorderFactory.createCompoundBorder(
                new RoundedBorder(12),
                BorderFactory.createEmptyBorder(20, 20, 20, 20)));

        // Header
        JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
        headerPanel.setOpaque(false);
        JLabel iconLabel = new JLabel(icon);
        iconLabel.setFont(FontUtils.getEmojiFont(Font.PLAIN, 20));
        iconLabel.setForeground(accentColor);
        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        titleLabel.setForeground(new Color(107, 114, 128));
        headerPanel.add(iconLabel);
        headerPanel.add(titleLabel);

        // Data
        dataLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 28));
        dataLabel.setForeground(new Color(55, 65, 81));
        dataLabel.setHorizontalAlignment(JLabel.CENTER);

        card.add(headerPanel, BorderLayout.NORTH);
        card.add(dataLabel, BorderLayout.CENTER);

        return card;
    }

    private static class RoundedBorder implements javax.swing.border.Border {
        private int radius;

        RoundedBorder(int radius) {
            this.radius = radius;
        }

        public Insets getBorderInsets(Component c) {
            return new Insets(this.radius + 1, this.radius + 1, this.radius + 2, this.radius);
        }

        public boolean isBorderOpaque() {
            return true;
        }

        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            g.drawRoundRect(x, y, width - 1, height - 1, radius, radius);
        }
    }

    private JLabel createStyledLabel(String text) {
        JLabel label = new JLabel(text);
        label.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        label.setForeground(new Color(75, 85, 99));
        return label;
    }

    private JLabel createStyledControlLabel(String text) {
        JLabel label = new JLabel(text);
        label.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        label.setForeground(new Color(75, 85, 99));
        return label;
    }

    private void styleButton(JButton button, Color backgroundColor, Color textColor) {
        button.setPreferredSize(new Dimension(100, 32));
        button.setBackground(backgroundColor);
        button.setForeground(textColor);
        button.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
        button.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(backgroundColor.darker(), 1),
                BorderFactory.createEmptyBorder(6, 12, 6, 12)));
        button.setFocusPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
    }

    private void styleComboBox(JComboBox<String> comboBox) {
        comboBox.setPreferredSize(new Dimension(120, 30));
        comboBox.setFont(new Font("Microsoft YaHei", Font.PLAIN, 13));
        comboBox.setBackground(Color.WHITE);
        comboBox.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(209, 213, 219), 1),
                BorderFactory.createEmptyBorder(4, 8, 4, 8)));
    }

    private JPanel createPremiumTrendChart() {
        JPanel panel = new JPanel() {
            private int hoveredPoint = -1;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                int width = getWidth() - 80;
                int height = getHeight() - 80;
                int startX = 40, startY = 40;

                // 背景渐变
                GradientPaint bgGradient = new GradientPaint(0, 0, new Color(250, 251, 255), 0, getHeight(),
                        new Color(245, 247, 255));
                g2d.setPaint(bgGradient);
                g2d.fillRect(0, 0, getWidth(), getHeight());

                // 网格线
                g2d.setColor(new Color(226, 232, 240));
                g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                for (int i = 0; i <= 5; i++) {
                    int y = startY + i * (height / 5);
                    g2d.drawLine(startX, y, width + startX, y);
                }

                String period = (String) periodComboBox.getSelectedItem();
                String[] timeLabels;
                int[] dataPoints;

                switch (period) {
                    case "近一周":
                        timeLabels = new String[] { "周一", "周二", "周三", "周四", "周五", "周六", "周日" };
                        dataPoints = new int[] { 2, 4, 3, 5, 6, 3, 4 };
                        break;
                    case "近一月":
                        timeLabels = new String[] { "第1周", "第2周", "第3周", "第4周" };
                        dataPoints = new int[] { 18, 25, 22, 28 };
                        break;
                    case "近一年":
                        timeLabels = new String[] { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月",
                                "12月" };
                        dataPoints = new int[] { 80, 75, 90, 85, 95, 100, 88, 92, 87, 90, 85, 88 };
                        break;
                    default:
                        timeLabels = new String[] { "周一", "周二", "周三", "周四", "周五", "周六", "周日" };
                        dataPoints = new int[] { 2, 4, 3, 5, 6, 3, 4 };
                }

                int maxValue = Math.max(10, java.util.Arrays.stream(dataPoints).max().orElse(0));

                // 绘制渐变填充区域
                if (dataPoints.length > 1) {
                    GeneralPath path = new GeneralPath();
                    path.moveTo(startX, height + startY);

                    for (int i = 0; i < dataPoints.length; i++) {
                        int x = startX + (i * width / Math.max(1, dataPoints.length - 1));
                        int y = height - (dataPoints[i] * height / maxValue) + startY;
                        if (i == 0)
                            path.lineTo(x, y);
                        else
                            path.lineTo(x, y);
                    }

                    path.lineTo(startX + width, height + startY);
                    path.closePath();

                    GradientPaint fillGradient = new GradientPaint(0, startY, new Color(99, 102, 241, 60), 0,
                            height + startY, new Color(99, 102, 241, 10));
                    g2d.setPaint(fillGradient);
                    g2d.fill(path);
                }

                // 绘制折线
                g2d.setColor(new Color(99, 102, 241));
                g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

                if (dataPoints.length > 1) {
                    for (int i = 0; i < dataPoints.length - 1; i++) {
                        int x1 = startX + (i * width / (dataPoints.length - 1));
                        int y1 = height - (dataPoints[i] * height / maxValue) + startY;
                        int x2 = startX + ((i + 1) * width / (dataPoints.length - 1));
                        int y2 = height - (dataPoints[i + 1] * height / maxValue) + startY;
                        g2d.drawLine(x1, y1, x2, y2);
                    }
                }

                // 绘制数据点
                for (int i = 0; i < dataPoints.length; i++) {
                    int x = startX + (i * width / Math.max(1, dataPoints.length - 1));
                    int y = height - (dataPoints[i] * height / maxValue) + startY;

                    if (i == hoveredPoint) {
                        // 外圈
                        g2d.setColor(new Color(99, 102, 241, 100));
                        g2d.fillOval(x - 8, y - 8, 16, 16);

                        // 内圈
                        g2d.setColor(new Color(99, 102, 241));
                        g2d.fillOval(x - 5, y - 5, 10, 10);

                        // 数值显示
                        g2d.setColor(new Color(30, 41, 59));
                        g2d.setFont(new Font("Microsoft YaHei", Font.BOLD, 12));
                        String valueText = dataPoints[i] + " 小时";
                        int textWidth = g2d.getFontMetrics().stringWidth(valueText);

                        // 背景气泡
                        g2d.setColor(new Color(255, 255, 255, 230));
                        g2d.fillRoundRect(x - textWidth / 2 - 5, y - 25, textWidth + 10, 20, 8, 8);
                        g2d.setColor(new Color(203, 213, 225));
                        g2d.drawRoundRect(x - textWidth / 2 - 5, y - 25, textWidth + 10, 20, 8, 8);

                        g2d.setColor(new Color(30, 41, 59));
                        g2d.drawString(valueText, x - textWidth / 2, y - 10);
                    } else {
                        g2d.setColor(Color.WHITE);
                        g2d.fillOval(x - 4, y - 4, 8, 8);
                        g2d.setColor(new Color(99, 102, 241));
                        g2d.setStroke(new BasicStroke(2));
                        g2d.drawOval(x - 4, y - 4, 8, 8);
                    }

                    // X轴标签
                    g2d.setColor(new Color(100, 116, 139));
                    g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
                    int labelWidth = g2d.getFontMetrics().stringWidth(timeLabels[i]);
                    g2d.drawString(timeLabels[i], x - labelWidth / 2, height + startY + 20);
                }

                // Y轴标签
                g2d.setColor(new Color(100, 116, 139));
                g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 10));
                for (int i = 0; i <= 5; i++) {
                    int value = (maxValue * i / 5);
                    int y = height - (i * height / 5) + startY;
                    g2d.drawString(value + "h", 5, y + 4);
                }
            }
        };

        // 鼠标交互
        panel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseMoved(java.awt.event.MouseEvent e) {
                String period = (String) periodComboBox.getSelectedItem();
                int dataLength = period.equals("近一年") ? 12 : (period.equals("近一月") ? 4 : 7);
                int width = panel.getWidth() - 80;
                int newHovered = -1;

                if (dataLength > 1) {
                    for (int i = 0; i < dataLength; i++) {
                        int x = 40 + (i * width / (dataLength - 1));
                        if (Math.abs(e.getX() - x) < 25) {
                            newHovered = i;
                            break;
                        }
                    }
                }

                Integer oldHovered = (Integer) panel.getClientProperty("hoveredPoint");
                if (oldHovered == null)
                    oldHovered = -1;
                if (newHovered != oldHovered) {
                    panel.putClientProperty("hoveredPoint", newHovered);
                    panel.repaint();
                }
            }
        });

        return panel;
    }

    private JPanel createPremiumPieChart() {
        JPanel panel = new JPanel() {
            private int hoveredSegment = -1;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 背景渐变
                GradientPaint bgGradient = new GradientPaint(0, 0, new Color(250, 251, 255), 0, getHeight(),
                        new Color(245, 247, 255));
                g2d.setPaint(bgGradient);
                g2d.fillRect(0, 0, getWidth(), getHeight());

                int centerX = getWidth() / 2 - 40;
                int centerY = getHeight() / 2;
                int radius = Math.max(50, Math.min(getWidth() - 300, getHeight() - 80) / 2);

                String[] timeSlots = { "上午时段", "下午时段", "晚上时段" };
                int[] values = { 25, 35, 40 };
                Color[] colors = {
                        new Color(16, 185, 129), // 青绿色
                        new Color(245, 158, 11), // 橙色
                        new Color(99, 102, 241) // 紫色
                };

                int startAngle = 90; // 从顶部开始

                // 绘制扇形
                for (int i = 0; i < timeSlots.length; i++) {
                    int arcAngle = (int) (360.0 * values[i] / 100);
                    int drawRadius = radius;
                    int drawCenterX = centerX;
                    int drawCenterY = centerY;

                    // 悬停效果
                    if (i == hoveredSegment) {
                        drawRadius += 10;
                        double midAngle = Math.toRadians(startAngle + arcAngle / 2.0);
                        drawCenterX += (int) (8 * Math.cos(midAngle));
                        drawCenterY -= (int) (8 * Math.sin(midAngle));
                    }

                    // 渐变填充
                    Color darkColor = colors[i];
                    Color lightColor = new Color(darkColor.getRed(), darkColor.getGreen(), darkColor.getBlue(), 180);

                    // 确保半径大于0
                    int safeRadius = Math.max(1, drawRadius);

                    RadialGradientPaint radialGradient = new RadialGradientPaint(
                            drawCenterX, drawCenterY, safeRadius,
                            new float[] { 0.0f, 1.0f },
                            new Color[] { darkColor, lightColor });

                    g2d.setPaint(radialGradient);
                    g2d.fillArc(drawCenterX - drawRadius, drawCenterY - drawRadius,
                            2 * drawRadius, 2 * drawRadius, startAngle, arcAngle);

                    // 边框
                    g2d.setColor(Color.WHITE);
                    g2d.setStroke(new BasicStroke(4));
                    g2d.drawArc(drawCenterX - drawRadius, drawCenterY - drawRadius,
                            2 * drawRadius, 2 * drawRadius, startAngle, arcAngle);

                    // 百分比标签
                    double midAngle = Math.toRadians(startAngle + arcAngle / 2.0);
                    int textX = drawCenterX + (int) (drawRadius * 0.65 * Math.cos(midAngle));
                    int textY = drawCenterY - (int) (drawRadius * 0.65 * Math.sin(midAngle));

                    g2d.setColor(Color.WHITE);
                    g2d.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
                    String percentage = values[i] + "%";
                    int strWidth = g2d.getFontMetrics().stringWidth(percentage);
                    g2d.drawString(percentage, textX - strWidth / 2, textY + 6);

                    startAngle += arcAngle;
                }

                // 图例
                int legendX = centerX + radius + 40;
                int legendY = centerY - (timeSlots.length * 30) / 2;

                g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
                for (int i = 0; i < timeSlots.length; i++) {
                    int y = legendY + i * 35;

                    // 颜色方块
                    g2d.setColor(colors[i]);
                    g2d.fillRoundRect(legendX, y - 8, 16, 16, 4, 4);
                    g2d.setColor(Color.WHITE);
                    g2d.drawRoundRect(legendX, y - 8, 16, 16, 4, 4);

                    // 文字
                    g2d.setColor(new Color(30, 41, 59));
                    g2d.drawString(timeSlots[i], legendX + 25, y + 4);

                    g2d.setColor(new Color(100, 116, 139));
                    g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
                    g2d.drawString(values[i] + "%", legendX + 25, y + 18);
                    g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
                }
            }
        };

        // 鼠标交互
        panel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseMoved(java.awt.event.MouseEvent e) {
                int centerX = panel.getWidth() / 2 - 40;
                int centerY = panel.getHeight() / 2;
                int radius = Math.max(50, Math.min(panel.getWidth() - 300, panel.getHeight() - 80) / 2);

                int dx = e.getX() - centerX;
                int dy = e.getY() - centerY;
                double distance = Math.sqrt(dx * dx + dy * dy);

                int newHovered = -1;
                if (distance <= radius + 10) {
                    double angle = Math.toDegrees(Math.atan2(-dy, dx)) + 90;
                    if (angle < 0)
                        angle += 360;

                    int[] values = { 25, 35, 40 };
                    int currentAngle = 0;
                    for (int i = 0; i < values.length; i++) {
                        int arcAngle = (int) (360.0 * values[i] / 100);
                        if (angle >= currentAngle && angle < currentAngle + arcAngle) {
                            newHovered = i;
                            break;
                        }
                        currentAngle += arcAngle;
                    }
                }

                Integer oldHovered = (Integer) panel.getClientProperty("hoveredSegment");
                if (oldHovered == null)
                    oldHovered = -1;
                if (newHovered != oldHovered) {
                    panel.putClientProperty("hoveredSegment", newHovered);
                    panel.repaint();
                }
            }
        });

        return panel;
    }

    private JPanel createStatCard(JLabel label, String icon) {
        JPanel card = new JPanel(new BorderLayout());
        card.setBackground(new Color(255, 255, 255));
        card.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(229, 231, 235), 1),
                BorderFactory.createEmptyBorder(15, 15, 15, 15)));
        JLabel iconLabel = new JLabel(icon);
        iconLabel.setFont(FontUtils.getEmojiFont(Font.PLAIN, 20));
        iconLabel.setHorizontalAlignment(JLabel.CENTER);
        label.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        label.setForeground(new Color(55, 65, 81));
        label.setHorizontalAlignment(JLabel.CENTER);
        card.add(iconLabel, BorderLayout.NORTH);
        card.add(label, BorderLayout.CENTER);
        return card;
    }

    /**
     * 创建方形统计卡片
     */
    private JPanel createSquareStatCard(JLabel dataLabel, String icon, String title, Color primaryColor,
            Color secondaryColor) {
        JPanel card = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 背景渐变
                GradientPaint gradient = new GradientPaint(
                        0, 0, Color.WHITE,
                        0, getHeight(), new Color(252, 252, 255));
                g2d.setPaint(gradient);
                g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16);

                // 左上角彩色装饰
                GradientPaint accentGradient = new GradientPaint(
                        0, 0, primaryColor,
                        getWidth() / 3, getHeight() / 3, secondaryColor);
                g2d.setPaint(accentGradient);
                g2d.fillRoundRect(0, 0, 60, 4, 4, 4);

                // 阴影效果
                g2d.setColor(new Color(0, 0, 0, 12));
                g2d.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 16, 16);
            }
        };

        card.setLayout(new BorderLayout(8, 8));
        card.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        card.setPreferredSize(new Dimension(180, 120)); // 方形比例

        // 顶部：图标和标题
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        topPanel.setOpaque(false);

        JLabel iconLabel = new JLabel(icon);
        iconLabel.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 20));
        iconLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 8));

        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
        titleLabel.setForeground(new Color(100, 116, 139));

        topPanel.add(iconLabel);
        topPanel.add(titleLabel);

        // 中部：数据
        dataLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 24));
        dataLabel.setForeground(new Color(30, 41, 59));
        dataLabel.setHorizontalAlignment(JLabel.LEFT);

        card.add(topPanel, BorderLayout.NORTH);
        card.add(dataLabel, BorderLayout.CENTER);

        // 悬停效果
        card.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseEntered(java.awt.event.MouseEvent e) {
                card.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(java.awt.event.MouseEvent e) {
                card.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
        });

        return card;
    }

    /**
     * 创建紧凑的趋势图表
     */
    private JPanel createCompactTrendChart() {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 背景
                g2d.setColor(Color.WHITE);
                g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 12, 12);

                int margin = 25;
                int width = getWidth() - 2 * margin;
                int height = getHeight() - 2 * margin - 30; // 留出标题空间

                // 获取真实数据
                String period = (String) periodComboBox.getSelectedItem();
                int[] dataPoints;
                try {
                    Map<String, Object> trendData = statisticsController.getUserTrendData(currentUser.getUserId(),
                            period);
                    dataPoints = (int[]) trendData.get("data");
                } catch (Exception e) {
                    // 使用默认数据作为fallback
                    switch (period) {
                        case "近一周":
                            dataPoints = new int[] { 0, 0, 0, 0, 0, 0, 0 };
                            break;
                        case "近一月":
                            dataPoints = new int[] { 0, 0, 0, 0 };
                            break;
                        case "近一年":
                            dataPoints = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                            break;
                        default:
                            dataPoints = new int[] { 0, 0, 0, 0, 0, 0, 0 };
                    }
                }

                int maxValue = Math.max(10, java.util.Arrays.stream(dataPoints).max().orElse(0));

                // 绘制背景区域
                if (dataPoints.length > 1) {
                    GeneralPath path = new GeneralPath();
                    path.moveTo(margin, height + margin + 30);

                    for (int i = 0; i < dataPoints.length; i++) {
                        int x = margin + (i * width / Math.max(1, dataPoints.length - 1));
                        int y = height - (dataPoints[i] * height / maxValue) + margin + 30;
                        if (i == 0)
                            path.lineTo(x, y);
                        else
                            path.lineTo(x, y);
                    }

                    path.lineTo(margin + width, height + margin + 30);
                    path.closePath();

                    GradientPaint fillGradient = new GradientPaint(0, margin + 30, new Color(16, 185, 129, 60),
                            0, height + margin + 30, new Color(16, 185, 129, 10));
                    g2d.setPaint(fillGradient);
                    g2d.fill(path);
                }

                // 绘制线条
                g2d.setColor(new Color(16, 185, 129));
                g2d.setStroke(new BasicStroke(2.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

                if (dataPoints.length > 1) {
                    for (int i = 0; i < dataPoints.length - 1; i++) {
                        int x1 = margin + (i * width / (dataPoints.length - 1));
                        int y1 = height - (dataPoints[i] * height / maxValue) + margin + 30;
                        int x2 = margin + ((i + 1) * width / (dataPoints.length - 1));
                        int y2 = height - (dataPoints[i + 1] * height / maxValue) + margin + 30;
                        g2d.drawLine(x1, y1, x2, y2);
                    }
                }

                // 绘制数据点
                for (int i = 0; i < dataPoints.length; i++) {
                    int x = margin + (i * width / Math.max(1, dataPoints.length - 1));
                    int y = height - (dataPoints[i] * height / maxValue) + margin + 30;

                    g2d.setColor(Color.WHITE);
                    g2d.fillOval(x - 4, y - 4, 8, 8);
                    g2d.setColor(new Color(16, 185, 129));
                    g2d.setStroke(new BasicStroke(2));
                    g2d.drawOval(x - 4, y - 4, 8, 8);
                }
            }
        };

        panel.setBackground(new Color(248, 250, 252));
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(229, 231, 235), 1),
                BorderFactory.createEmptyBorder(15, 0, 15, 0)));

        // 添加标题
        JLabel titleLabel = new JLabel("预约趋势", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        titleLabel.setForeground(new Color(75, 85, 99));

        panel.setLayout(new BorderLayout());
        panel.add(titleLabel, BorderLayout.NORTH);

        return panel;
    }

    /**
     * 创建紧凑的饼图
     */
    private JPanel createCompactPieChart() {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 背景
                g2d.setColor(Color.WHITE);
                g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 12, 12);

                int centerX = getWidth() / 2;
                int centerY = (getHeight() + 30) / 2; // 考虑标题空间
                int radius = Math.min(getWidth() - 80, getHeight() - 100) / 2;

                String[] timeSlots = { "上午", "下午", "晚上" };
                int[] values;
                try {
                    String period = (String) periodComboBox.getSelectedItem();
                    Map<String, Object> timeSlotData = statisticsController.getUserTimeSlotData(currentUser.getUserId(),
                            period);
                    values = (int[]) timeSlotData.get("values");
                } catch (Exception e) {
                    // 使用默认数据作为fallback
                    values = new int[] { 0, 0, 0 };
                }
                Color[] colors = {
                        new Color(34, 197, 94), // 绿色
                        new Color(245, 158, 11), // 橙色
                        new Color(59, 130, 246) // 蓝色
                };

                int startAngle = 90; // 从顶部开始
                for (int i = 0; i < timeSlots.length; i++) {
                    int arcAngle = (int) (360.0 * values[i] / 100);

                    g2d.setColor(colors[i]);
                    g2d.fillArc(centerX - radius, centerY - radius,
                            2 * radius, 2 * radius, startAngle, arcAngle);

                    // 边框
                    g2d.setColor(Color.WHITE);
                    g2d.setStroke(new BasicStroke(2));
                    g2d.drawArc(centerX - radius, centerY - radius,
                            2 * radius, 2 * radius, startAngle, arcAngle);

                    // 百分比标签
                    double midAngle = Math.toRadians(startAngle + arcAngle / 2.0);
                    int textX = centerX + (int) (radius * 0.6 * Math.cos(midAngle));
                    int textY = centerY - (int) (radius * 0.6 * Math.sin(midAngle));

                    g2d.setColor(Color.WHITE);
                    g2d.setFont(new Font("Microsoft YaHei", Font.BOLD, 12));
                    String percentage = values[i] + "%";
                    int stringWidth = g2d.getFontMetrics().stringWidth(percentage);
                    g2d.drawString(percentage, textX - stringWidth / 2, textY + 4);

                    startAngle += arcAngle;
                }

                // 简化的图例
                int legendY = getHeight() - 40;
                int legendSpacing = getWidth() / timeSlots.length;
                g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 10));
                for (int i = 0; i < timeSlots.length; i++) {
                    int x = (i * legendSpacing) + legendSpacing / 2 - 20;
                    g2d.setColor(colors[i]);
                    g2d.fillRoundRect(x, legendY, 10, 10, 3, 3);

                    g2d.setColor(new Color(75, 85, 99));
                    g2d.drawString(timeSlots[i], x + 15, legendY + 9);
                }
            }
        };

        panel.setBackground(new Color(248, 250, 252));
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(229, 231, 235), 1),
                BorderFactory.createEmptyBorder(15, 0, 15, 0)));

        // 添加标题
        JLabel titleLabel = new JLabel("时间段分布", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        titleLabel.setForeground(new Color(75, 85, 99));

        panel.setLayout(new BorderLayout());
        panel.add(titleLabel, BorderLayout.NORTH);

        return panel;
    }

    /**
     * 创建信誉分图表
     */
    private JPanel createCreditChart() {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 背景
                g2d.setColor(Color.WHITE);
                g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 12, 12);

                int centerX = getWidth() / 2;
                int centerY = (getHeight() + 30) / 2;
                int outerRadius = Math.min(getWidth() - 60, getHeight() - 90) / 2;

                // 获取真实信誉分数据
                int creditScore;
                try {
                    creditScore = statisticsController.getUserCreditScore(currentUser.getUserId());
                } catch (Exception e) {
                    creditScore = 100; // 默认初始分数
                }
                double percentage = creditScore / 150.0; // 满分150

                // 背景圆环
                g2d.setColor(new Color(243, 244, 246));
                g2d.setStroke(new BasicStroke(15, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                g2d.drawArc(centerX - outerRadius, centerY - outerRadius,
                        2 * outerRadius, 2 * outerRadius, 0, 360);

                // 信誉分圆环
                Color creditColor = creditScore > 100 ? new Color(34, 197, 94) : // 绿色(优秀,>100)
                        creditScore >= 80 ? new Color(245, 158, 11) : // 橙色(良好,80-100)
                                new Color(239, 68, 68); // 红色(需改善,<80)

                g2d.setColor(creditColor);
                int arcAngle = (int) (360 * percentage);
                g2d.drawArc(centerX - outerRadius, centerY - outerRadius,
                        2 * outerRadius, 2 * outerRadius, 90, -arcAngle);

                // 中心分数显示
                g2d.setColor(new Color(30, 41, 59));
                g2d.setFont(new Font("Microsoft YaHei", Font.BOLD, 24));
                String scoreText = String.valueOf(creditScore);
                int scoreWidth = g2d.getFontMetrics().stringWidth(scoreText);
                g2d.drawString(scoreText, centerX - scoreWidth / 2, centerY - 5);

                g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
                g2d.setColor(new Color(107, 114, 128));
                String unitText = "/ 150分";
                int unitWidth = g2d.getFontMetrics().stringWidth(unitText);
                g2d.drawString(unitText, centerX - unitWidth / 2, centerY + 15);

                // 评级文字
                String rating = creditScore > 100 ? "优秀" : creditScore >= 80 ? "良好" : "需改善";
                g2d.setFont(new Font("Microsoft YaHei", Font.BOLD, 12));
                g2d.setColor(creditColor);
                int ratingWidth = g2d.getFontMetrics().stringWidth(rating);
                g2d.drawString(rating, centerX - ratingWidth / 2, centerY + 35);
            }
        };

        panel.setBackground(new Color(248, 250, 252));
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(229, 231, 235), 1),
                BorderFactory.createEmptyBorder(15, 0, 15, 0)));

        // 添加标题
        JLabel titleLabel = new JLabel("信誉分", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        titleLabel.setForeground(new Color(75, 85, 99));

        panel.setLayout(new BorderLayout());
        panel.add(titleLabel, BorderLayout.NORTH);

        return panel;
    }

    /**
     * 创建考勤图表
     */
    private JPanel createAttendanceChart() {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 背景
                g2d.setColor(Color.WHITE);
                g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 12, 12);

                int margin = 20;
                int width = getWidth() - 2 * margin;
                int height = getHeight() - margin - 60; // 留出标题和标签空间

                // 获取真实考勤数据
                String[] labels = { "正常", "迟到", "早退", "爽约" };
                int[] values;
                try {
                    String period = (String) periodComboBox.getSelectedItem();
                    Map<String, Object> attendanceData = statisticsController
                            .getUserAttendanceData(currentUser.getUserId(), period);
                    values = (int[]) attendanceData.get("values");
                } catch (Exception e) {
                    // 使用默认数据作为fallback
                    values = new int[] { 0, 0, 0, 0 };
                }
                Color[] colors = {
                        new Color(34, 197, 94), // 绿色(正常)
                        new Color(245, 158, 11), // 橙色(迟到)
                        new Color(168, 85, 247), // 紫色(早退)
                        new Color(239, 68, 68) // 红色(爽约)
                };

                int maxValue = Math.max(1, java.util.Arrays.stream(values).max().orElse(1));
                int barWidth = Math.max(1, width / (labels.length * 2));

                // 绘制柱状图
                for (int i = 0; i < labels.length; i++) {
                    int x = margin + i * (width / labels.length) + (width / labels.length - barWidth) / 2;
                    int barHeight = Math.max(1, (values[i] * height / maxValue));
                    int y = margin + 30 + height - barHeight;

                    // 绘制柱子
                    g2d.setColor(colors[i]);
                    g2d.fillRoundRect(x, y, barWidth, barHeight, 4, 4);

                    // 数值标签
                    g2d.setColor(new Color(75, 85, 99));
                    g2d.setFont(new Font("Microsoft YaHei", Font.BOLD, 11));
                    String valueText = String.valueOf(values[i]);
                    int textWidth = g2d.getFontMetrics().stringWidth(valueText);
                    g2d.drawString(valueText, x + (barWidth - textWidth) / 2, y - 5);

                    // X轴标签
                    g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 10));
                    int labelWidth = g2d.getFontMetrics().stringWidth(labels[i]);
                    g2d.drawString(labels[i], x + (barWidth - labelWidth) / 2, margin + height + 45);
                }

                // 绘制Y轴刻度
                g2d.setColor(new Color(156, 163, 175));
                g2d.setStroke(new BasicStroke(1));
                for (int i = 0; i <= 3; i++) {
                    int value = (maxValue * i / 3);
                    int y = margin + 30 + height - (i * height / 3);
                    g2d.drawLine(margin - 5, y, margin, y);

                    g2d.setFont(new Font("Microsoft YaHei", Font.PLAIN, 9));
                    g2d.drawString(String.valueOf(value), margin - 15, y + 3);
                }
            }
        };

        panel.setBackground(new Color(248, 250, 252));
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(229, 231, 235), 1),
                BorderFactory.createEmptyBorder(15, 0, 15, 0)));

        // 添加标题
        JLabel titleLabel = new JLabel("考勤统计", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        titleLabel.setForeground(new Color(75, 85, 99));

        panel.setLayout(new BorderLayout());
        panel.add(titleLabel, BorderLayout.NORTH);

        return panel;
    }
}
